<template>
  <a-modal :title="title"
           :width="800"
           :visible="visible"
           :confirmLoading="confirmLoading"
           @ok="handleSubmit"
           @cancel="handleCancel"
           cancelText="取消"
           okText="保存"
           :okButtonProps="{ props: {disabled: !showOk} }"
           :maskClosable=false
           :destroyOnClose=true>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="编 号"
                         :labelCol="labelCol"
                         :wrapperCol="wrapperCol">
              <a-input v-decorator="['code',{rules: [{ required: true, message: '请输入人员编号' }]}]"
                       name="code"
                       placeholder="请输入人员编号" :disabled="isEditOrView" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="姓 名"
                         :labelCol="labelCol"
                         :wrapperCol="wrapperCol">
              <a-input v-decorator="['name',{rules: [{ required: true, message: '请输入姓名' }]}]"
                       name="name"
                       placeholder="请输入名字" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col span="12">
            <a-form-item label="性 别"
                         :labelCol="labelCol"
                         :wrapperCol="wrapperCol"
                         key="gender">
              <a-select placeholder="请选择一个性别"
                        v-decorator="['gender',{rules:[{required: true, message: '请选择性别'}]}]">
                <a-select-option value="0">男性</a-select-option>
                <a-select-option value="1">女性</a-select-option>
                <a-select-option value="2">未知</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="身份证"
                         :labelCol="labelCol"
                         :wrapperCol="wrapperCol">
              <a-input v-decorator="['cardNo',{rules: [{ required: true,max: 18,min: 18, message: '必输项，且18位' }]}]"
                       name="cardNo"
                       placeholder="请输入18位身份证号码" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col span="12">
            <a-form-item label="出生日期"
                         :labelCol="labelCol"
                         :wrapperCol="wrapperCol"
                         key="birthday">
              <a-date-picker style="width: 100%"
                             placeholder="请输入出生日期"
                             v-decorator="['birthday',{}]" />
            </a-form-item>
          </a-col>
          <a-col span="12">
            <a-form-item label="状 态"
                         :labelCol="labelCol"
                         :wrapperCol="wrapperCol"
                         key="status">
              <a-select placeholder="请选择在职状态"
                        v-decorator="['status',{}]">
                <a-select-option value="0">在职</a-select-option>
                <a-select-option value="1">停职</a-select-option>
                <a-select-option value="2">离职</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col span="24">
            <a-form-item label="联系电话"
                         :labelCol="labelColTwo"
                         :wrapperCol="wrapperColTwo"
                         key="phone">
              <a-input placeholder="请输入电话"
                       v-decorator="['phone',{}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col span="24">
            <a-form-item label="电子邮箱"
                         :labelCol="labelColTwo"
                         :wrapperCol="wrapperColTwo"
                         key="email">
              <a-input placeholder="请输入Email"
                       v-decorator="['email',{}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col span="24">
            <a-form-item label="备注"
                         :labelCol="labelColTwo"
                         :wrapperCol="wrapperColTwo"
                         key="remark">
              <a-textarea placeholder="请输入备注信息"
                       v-decorator="['remark',{}]" :autosize="{ minRows: 4, maxRows: 8 }"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col span="12">
            <a-form-item key="id" class="un-display" >
              <a-input v-decorator="['id',{}]"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getItem, submitItem } from '@/api/user'
export default {
  data () {
    return {
      labelCol: {
        xs: { span: 8 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 16 },
        sm: { span: 16 }
      },
      labelColTwo: {
        xs: { span: 8 },
        sm: { span: 4 }
      },
      wrapperColTwo: {
        xs: { span: 16 },
        sm: { span: 20 }
      },
      visible: false,
      confirmLoading: false,
      editType: 'view',
      id: '',
      formValue: {},
      hasRender: false,
      form: this.$form.createForm(this)
    }
  },
  computed: {
    showOk () {
      return this.editType === 'edit' || this.editType === 'add'
    },
    isEditOrView () {
      return this.editType === 'edit' || this.editType === 'view'
    },
    title () {
      if (this.editType === 'add') {
        return '新增'
      } else if (this.editType === 'edit') {
        return '修改'
      } else {
        return '查看'
      }
    }
  },
  updated () {
    if (this.visible && !this.hasRender) {
      if (this.editType === 'edit' || this.editType === 'view') {
        this.fetch(this.id)
        this.hasRender = true
      }
      if (this.editType === 'add') {
        this.creaetInit()
        this.hasRender = true
      }
    }
  },
  methods: {
    init (params) {
      this.editType = params.editType
      this.hasRender = false
      this.visible = true
      this.id = params.id
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          if (values.birthday) {
            values.birthday = this.$moment(values.birthday).format('YYYY-MM-DD')
          }
          if (values.gender) {
            values.gender = Number(values.gender)
          }
          if (values.status) {
            values.status = Number(values.status)
          }

          submitItem(values).then((data) => {
            let ret = data.data
            if (ret.success === true) {
              this.visible = false
              this.confirmLoading = false
              this.$emit('ok', values)
            } else {
              this.$error({
                title: '保存失败',
                content: `发生了以下错误：${ret.message}`
              })
            }
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
    },
    fetch (id) {
      getItem({ id: id }).then((data) => {
        let ret = data.data
        if (ret.success === true) {
          ret.results.birthday = this.$moment(ret.results.birthday)
          ret.results.gender = ret.results.gender !== undefined ? Number(ret.results.gender).toString() : undefined
          ret.results.status = ret.results.status !== undefined ? Number(ret.results.status).toString() : undefined
          this.formValue = ret.results
          this.form.setFieldsValue(this.formValue)
        } else {
          this.$error({
            title: '获取数据失败',
            content: `发生了以下错误：${ret.message}`
          })
          this.visible = false
        }
      })
    },
    creaetInit () {
      let data = {
        code: '20190000',
        gender: '0',
        status: '0',
        birthday: this.$moment('2000-01-01')
      }
      this.formValue = data
      this.form.setFieldsValue(this.formValue)
    }
  }
}
</script>
<style lang="less">
.un-display{
  display: none
}
</style>
